/*
 * calibration_callback.c
 *
 *  Created on: 2022年02月10日
 *      Author: Moon
 */


/* Includes ------------------------------------------------------------------*/
#include "ParmManage.h"
#include "cmd_process.h"
#include "main_task.h"
#include "calibrate_dac_task.h"
/* Private defines -----------------------------------------------------------*/

/* Public enum    -----------------------------------------------------------*/

/* Public Struct  -----------------------------------------------------------*/
extern TaskHandle_t CaliProvaleTask_Handler; 

/* Private Variable  ---------------------------------------------------------*/
//Pointer

//Array

//Const

/* Private function prototypes -----------------------------------------------*/

SensorCalibration calibrations[11] = {
{1.0,0.0},//S1压力传感器矫正
{1.0,0.0},//S2压力传感器矫正
{1.0,0.0},//S3压力传感器矫正
{1.0,0.0},//P0压力传感器矫正

//如果手动标定，替换calibrations[PRE_SENSOR_BASE].intercept（截距）和 calibrations[PRE_SENSOR_BASE].slope（斜率）的值
//1.0对应斜率 0.0对应截距
{1.0,0.0},//基体腔压力传感器矫正 


{1.0,0.0},//温度传感器矫正

{1.0,0.0},//5个读取的压力参考值
{1.0,0.0},
{1.0,0.0},
{1.0,0.0},
{1.0,0.0},
};

/***********************************************************************************
 * @brief 读取温度传感器校准参数
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
void read_tempsensors_calibration(void) 
{
	float b = 0; 
	PPItemRead(PP_ADC1_INTERCEPT_CDT,(uint8_t*)&b,sizeof(uint32_t));
	calibrations[5].intercept = b;
}

/***********************************************************************************
 * @brief 读取压力传感器校准参数
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
void read_all_sensors_calibration(void) 
{
	float k = 1;
	float b = 0;

	for (int i = 0; i < 6; i++) 
	{
        // 读取每个传感器的斜率
        PPItemRead(PP_PRESENSOR1_SLOPE_CDT + i*2, (uint8_t*)&k , sizeof(uint32_t));
        
        if(k == 0)
        {
            k = 1;
        }
        
        calibrations[i].slope = (float) k;
        
        // 读取每个传感器的截距
        PPItemRead(PP_PRESENSOR1_INTERCEPT_CDT + i*2, (uint8_t*)&b, sizeof(uint32_t));
        calibrations[i].intercept = (float) b;

        if (k > 2.0)
        {
            k = 1.0;
        }

        if (b > 500 || b < (-500))
        {
            b = 100.0;
        }        

	}
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_dac_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_DAC_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_DAC_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_DAC_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_DAC_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_presAdc_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_ADC1_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_ADC1_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_ADC1_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_ADC1_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_tempsensors_calibration();
	return ack;
}


/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_tempAdc_callback(user_packet_t *user)
{
	ack_packet_t *ack;
	float temp = 0; 
	float *data;
	float b;
	
	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		data = (float *)&user->data[0];
		b = *(data + 1) - data_server.lastTemp;
		PPItemIWrite(PP_ADC1_INTERCEPT_CDT,(uint8_t*)&b,sizeof(uint32_t));		
		
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(4);
		PPItemRead(PP_ADC1_INTERCEPT_CDT, 	&ack->data[0],  4);
	}
    read_tempsensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preS1_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR1_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR1_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR1_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR1_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_all_sensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preS2_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR2_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR2_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR2_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR2_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_all_sensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preS3_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR3_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR3_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR3_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR3_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_all_sensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preP0_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR4_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR4_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR4_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR4_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_all_sensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preAmber_callback(user_packet_t *user)
{
	ack_packet_t *ack;
    

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR5_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR5_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR5_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR5_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	read_all_sensors_calibration();
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_provalve_callback(user_packet_t *user)
{
	ack_packet_t *ack;
    uint8_t cali_cmd = 0; //1:开始 2：停止
    
    cali_cmd = user->data[0];
	if (user->style == CMD_SET) //设置
	{
        // 启动比例阀DAC校准
//		if (0x01 == cali_cmd)
//        {       
//             psm = PSM_CALI_DAC;   
//             vTaskResume(CaliProvaleTask_Handler);   
//        }
//        // 启动比例阀反馈校准
//        else if (0x02 == cali_cmd)
//        {
//             psm = PSM_CALI_FEEDBACK;   
//             vTaskResume(CaliProvaleTask_Handler); 
//        
//        }
//        // 启动比例阀最小步进校准
//        else if (0x03 == cali_cmd)
//        {
//             psm = PSM_CALI_MINSTEP;   
//             vTaskResume(CaliProvaleTask_Handler); 
//        
//        }
//        // 停止比例阀校准
//        else if (0x04 == cali_cmd)
//        {
//             psm = PSM_CALI_STOP;   
//             vTaskResume(CaliProvaleTask_Handler); 
//        }   

        psm = PSM_CALI_DAC;
        vTaskResume(CaliProvaleTask_Handler); 
        
        // 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	
	
	return ack;
}
/***********************************************************************************
 * @brief notify base to vacuum in calibrate adc press process
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preAmber_to_vacuum_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR5_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR5_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR5_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR5_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	return ack;
}

/***********************************************************************************
 * @brief notify base to atoms in calibrate adc press process
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *cail_preAmber_to_atoms_callback(user_packet_t *user)
{
	ack_packet_t *ack;

	if (user->style == CMD_SET) //设置
	{
		// 用户数据处理
		PPItemIWrite(PP_PRESENSOR5_SLOPE_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_PRESENSOR5_INTERCEPT_CDT, &user->data[4],  4);
		// 应答返回数据
		ack = Ack_Malloc(1);
		ack->data[0] = 0x00;
	}
	else if (user->style == CMD_READ) //读取
	{
		// 应答返回数据
		ack = Ack_Malloc(8);
		PPItemRead(PP_PRESENSOR5_SLOPE_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_PRESENSOR5_INTERCEPT_CDT, 	&ack->data[4],  4);
	}
	return ack;
}
/******************* (C) COPYRIGHT 2021 CIQTEK Samuel *****END OF FILE****/

